********************
***** FIGURE 1 *****
********************
clear
use "$P_Data_Processed/adopter_measures_4week_e5.dta"
format avgdate %tw
histogram avgdate, xtitle("Date") ytitle("Percent") color(gray)
gr export "$P_Figures/Figure_1.eps", replace 


********************
***** FIGURE 2 *****
********************
graph drop _all
** N PRICE CHANGES

clear
use "$P_Data/break_pccount/weekly_nPCCount_e5.dta"

merge m:1 y2kW using  "$P_Data_Processed/week_y2kW_concordance.dta"
keep if _merge==3
drop _merge

gen year_month =mofd(dofw(weekly_date))
gener month = month(dofw(weekly_date))

gcollapse PCCount year month, by(StID year_month)

merge m:1 StID using "$P_Data/GS/gas_station.dta"
drop if _merge==2
drop _merge

rename PCCount avgnPC 

capture drop postal_n_changes
by year_month Post, sort: egen postal_n_changes = total(avgnPC)
capture drop postal_n_stations
by year_month Post: gener postal_n_stations = _N
capture drop postal_others_n_changes
gener postal_others_n_changes = (postal_n_changes - avgnPC)/(postal_n_stations)



merge m:1 StID using "$P_Data_Processed/pccount_breaks_e5.dta"
drop if _merge==2

gener pccount_break = (_merge==3)

gener year_month_shocked = year_month*(_merge==3)
gener l_avgnPC = ln(avgnPC)

drop _merge

merge m:1 StID using "$P_Data_Processed/adopter_measures_4week_e5.dta"

gener adopter = (_merge==3)


gener year_month_shocked_adopter = year_month_shocked*(_merge==3)

gener year_month_adopter = year_month*(adopter==1)

reghdfe l_avgnPC i.year_month ib672.year_month ib672.year_month_adopter postal_n_stations postal_others_n_changes  , absorb(StID) cluster(BrandID)


preserve

parmest, stars(0.1 0.05 0.01) level(95) norestore fast
replace parm = subinstr(parm,".year_month_adopter", "",.)
destring parm, replace force


drop if parm==.

format parm %tm

replace estimate = estimate*100
replace min95 = min95*100
replace max95 = max95*100

tw  (rcap min95 max95 parm, color(gray)) ///
	(line estimate parm, lcolor(dkgray )) ///
	(sc estimate parm, m(O) mfcolor(dkgray) ///
	mlcolor(gray) mlwidth(.4)), legend (label(1 "95 Percent CI")) xtitle("Date") legend(off) name(pccount_changes_adopt) title("(a) Outcome: ln(N Price Changes per Day)",size(medsmall))
 
gr export "$P_Figures/pct_break_diff_pccount_adopter.eps", replace

restore


** RIVAL RESPONSE TIME

clear
use "$P_Data/break_pcresponse/weekly_nPCResponse_e5.dta"

merge 1:1 StID y2kW using "$P_Data/PCResponse/rival_res-agg-e5.dta"
keep if _merge==3
drop _merge

merge m:1 y2kW using  "$P_Data_Processed/week_y2kW_concordance.dta"
keep if _merge==3
drop _merge

gen year_month =mofd(dofw(weekly_date))
gener month = month(dofw(weekly_date))

gcollapse Response nPCMe nPCRival year month, by(StID year_month)


merge m:1 StID using "$P_Data/GS/gas_station.dta"
drop if _merge==2
drop _merge


capture drop postal_n_changes
by year_month Post, sort: egen postal_n_changes = total(nPCMe)
capture drop postal_n_stations
by year_month Post: gener postal_n_stations = _N
capture drop postal_others_n_changes
gener postal_others_n_changes = (postal_n_changes - nPCMe)/(postal_n_stations)

merge m:1 StID using  "$P_Data_Processed/pcresponse_breaks_e5.dta"
drop if _merge==2

gener treat_group = (_merge==3)

gener year_month_shocked = year_month*(_merge==3)
drop _merge

merge m:1 StID using "$P_Data_Processed/adopter_measures_4week_e5.dta"

gener adopter = (_merge==3)


gener year_month_shocked_adopter = year_month_shocked*(_merge==3)

gener year_month_adopter = year_month*(adopter==1)

capture drop l_Response 
gener l_Response = -ln(Response)


reghdfe l_Response i.year_month ib672.year_month_adopter nPCMe nPCRival postal_n_stations if  year_month>672, absorb(StID) cluster(BrandID)

preserve

parmest, stars(0.1 0.05 0.01) level(95) norestore fast
replace parm = subinstr(parm,".year_month_adopter", "",.)
destring parm, replace force


drop if parm==.
format parm %tm


replace estimate = estimate*100
replace min95 = min95*100
replace max95 = max95*100

tw  (rcap min95 max95 parm, color(gray)) ///
	(line estimate parm, lcolor(dkgray )) ///
	(sc estimate parm, m(O) mfcolor(dkgray) ///
	mlcolor(gray) mlwidth(.4)), legend (label(1 "95 Percent CI")) xtitle("Date") legend(off)  name(pcresponse_changes_adopt) title("(b) Outcome: ln(Response Time)",size(medsmall))
 
gr export "$P_Figures/pct_break_diff_pcresponse_adopter.eps", replace

restore



** RESPONSE TO OIL SHOCK 
clear
use "$P_Data/break_oil/e5_shock_5min_P90_response_5min_v4.dta", clear

merge m:1 y2kW using  "$P_Data_Processed/week_y2kW_concordance.dta"
keep if _merge==3
drop _merge

gen year_month =mofd(dofw(weekly_date))
gener month = month(dofw(weekly_date))

gcollapse nRes nShock nPC year month, by(StID year_month)

merge m:1 StID using "$P_Data/GS/gas_station.dta"
drop if _merge==2
drop _merge


merge m:1 StID using  "$P_Data_Processed/oil_breaks_e5.dta"

gener treat_group = (_merge==3)

gener year_month_shocked = year_month*(_merge==3)
drop _merge

merge m:1 StID using "$P_Data_Processed/adopter_measures_4week_e5.dta"

gener adopter = (_merge==3)


gener year_month_shocked_adopter = year_month_shocked*(_merge==3)

gener year_month_adopter = year_month*(adopter==1)


capture drop l_nRes
gener l_nRes = -ln(nRes+1)

reghdfe l_nRes i.year_month ib672.year_month_adopter nShock nPC , absorb(StID) cluster(BrandID)
preserve

parmest, stars(0.1 0.05 0.01) level(95) norestore fast
replace parm = subinstr(parm,".year_month_adopter", "",.)
destring parm, replace force


drop if parm==.
format parm %tm


replace estimate = estimate*100
replace min95 = min95*100
replace max95 = max95*100

tw  (rcap min95 max95 parm, color(gray)) ///
	(line estimate parm, lcolor(dkgray )) ///
	(sc estimate parm, m(O) mfcolor(dkgray) ///
	mlcolor(gray) mlwidth(.4)), legend (label(1 "95 Percent CI")) xtitle("Date") legend(off) name(oil_changes_adopt) title("(d) Outcome: ln(N Responses to Crude Oil Shocks)",size(medsmall))
 
gr export "$P_Figures/pct_break_diff_oil_adopter.eps", replace

restore


** RESPONSE TO WEATHER SHOCK 

clear

clear
use "$P_Data/break_weather/temp_neg_wi10m_e5.dta", clear

merge m:1 y2kW using  "$P_Data_Processed/week_y2kW_concordance.dta"
keep if _merge==3
drop _merge

gen year_month =mofd(dofw(weekly_date))
gener month = month(dofw(weekly_date))

gcollapse nPC nShock nRes year month, by(StID year_month)


merge m:1 StID using "$P_Data/GS/gas_station.dta"
drop if _merge==2
drop _merge


merge m:1 StID using  "$P_Data_Processed/weather_breaks_e5.dta"

drop if _merge==2


gener treat_group = (_merge==3)

gener year_month_shocked = year_month*(_merge==3)
drop _merge

merge m:1 StID using "$P_Data_Processed/adopter_measures_4week_e5.dta"

gener adopter = (_merge==3)


gener year_month_shocked_adopter = year_month_shocked*(_merge==3)

gener year_month_adopter = year_month*(adopter==1)

capture drop l_nRes
gener l_nRes = ln(nRes)

reghdfe l_nRes i.year_month ib672.year_month_adopter nShock nPC , absorb(StID) cluster(BrandID)

preserve

parmest, stars(0.1 0.05 0.01) level(95) norestore fast
replace parm = subinstr(parm,".year_month_adopter", "",.)
destring parm, replace force


drop if parm==.
format parm %tm


replace estimate = estimate*100
replace min95 = min95*100
replace max95 = max95*100

tw  (rcap min95 max95 parm, color(gray)) ///
	(line estimate parm, lcolor(dkgray )) ///
	(sc estimate parm, m(O) mfcolor(dkgray) ///
	mlcolor(gray) mlwidth(.4)), legend (label(1 "95 Percent CI")) xtitle("Date") legend(off) name(weather_changes_adopt) title("(c) Outcome: ln(N Responses to Weather Shocks)", size(medsmall))
 
gr export "$P_Figures/pct_break_diff_weather_adopter.eps", replace 

restore


graph combine pccount_changes_adopt pcresponse_changes_adopt weather_changes_adopt oil_changes_adopt

gr export "$P_Figures/Figure_2.eps", replace


********************
***** FIGURE 3 *****
********************

graph drop _all

clear 

use "$P_Data_Processed/monthly_station_data_e5.dta", clear

by year_month Post, sort: gener postal_n_stations = _N
gener postal_n_others = postal_n_stations-1
drop if Post==0

** defining treatment 

capture drop treat
gener treat = month_treat_period*treat_group
label variable treat "Adopter" 

**
** drop outlier observations (top and bottom 1%)
drop if mean_wh_margin<0
drop if mean_wh_margin>0.2
drop if mean_price<1.18
drop if mean_price>1.7
	

sort StID year_month
by StID: gener first_treat_month = year_month if treat==1 & treat[_n-1]==0
sort StID first_treat_month
by StID: replace first_treat_month = first_treat_month[1]
replace first_treat_month = 1000 if missing(first_treat_month)
	
* number of other adopters in your Postal code
by Post year_month, sort: egen n_compet_adopt = total(treat)
replace n_compet_adopt = n_compet_adopt - 1 if treat==1	
	
** generating simple IV - share of adopters in your brand
by Brand year_month, sort: gener n_brand = _N
by Brand year_month: egen n_treat = total(treat)

gener share_others_treated = (n_treat)/(n_brand-1) if treat==0
replace share_others_treated = (n_treat-1)/(n_brand-1) if treat==1
label variable n_brand "N Brand Stations"
label variable share_others_treated "Share Brand Adopters"
label variable postal_n_stations "N Competitors in ZIP"
label variable n_compet_adopt "N Competitors Adopting"
*** regressions on margins
	
merge m:1 StID using "$P_Data_Processed/cluster_mkts.dta"
keep if _merge==3
drop _merge

gener station = 1 
gegen n_act_stations = total(station), by(mktid year_month)


gegen n_mkt_compet_adopt = total(treat), by(year_month mktid)
replace n_mkt_compet_adopt = n_mkt_compet_adopt - 1 if treat==1


by Brand year_month, sort: gener n_stations = _N
by Brand year_month, sort: egen n_adopters = total(treat)

by Brand year_month, sort: keep if _n==1

merge m:1 Brand using "$P_Data_Processed/top5_brands.dta"

gener share_adopters = n_adopter/n_stations
* december 2016
sum share_adopters if _merge==3 & year==2016 & month==12 & n_stations>=2
sum share_adopters if _merge!=3 & year==2016 & month==12 & n_stations>=2
* december 2017
sum share_adopters if _merge==3 & year==2017 & month==12 & n_stations>=2
sum share_adopters if _merge!=3 & year==2017 & month==12 & n_stations>=2
* december 2018
sum share_adopters if _merge==3 & year==2018 & month==12 & n_stations>=2
sum share_adopters if _merge!=3 & year==2018 & month==12 & n_stations>=2


format year_month %tm
tw (line share_adopters year_month if Brand=="shell", lcolor(gray))  ///
   (sc share_adopters year_month if Brand=="shell", m(O) mfcolor(gray) mlcolor(gray) msymbol(circle) mlwidth(0.1)) ///
 (line share_adopters year_month if Brand=="esso" , lcolor(gray)) ///
    (sc share_adopters year_month if Brand=="esso", m(O) mfcolor(gray) mlcolor(gray) msymbol(square) mlwidth(0.1)) ///
 (line share_adopters year_month if Brand=="aral", lcolor(gray)) ///
   (sc share_adopters year_month if Brand=="aral", m(O) mfcolor(gray) mlcolor(gray) msymbol(triangle) mlwidth(0.1)) ///
   (line share_adopters year_month if Brand=="total", lcolor(gray)) /// 
      (sc share_adopters year_month if Brand=="total", m(O) mfcolor(gray) mlcolor(gray) msymbol(diamond) mlwidth(0.1)) ///
 (line share_adopters year_month if Brand=="avia", lcolor(gray)) ///
      (sc share_adopters year_month if Brand=="avia", m(O) mfcolor(gray) mlcolor(gray) msymbol(X) mlwidth(0.4)) ///
 , ytitle("Share of Adopting Stations") legend(order(2 "Shell" 4 "Esso" 6 "Aral" 8 "Total" 10 "Avia")) xtitle("Month") title("(a) Top 5 Brand Shares", size(medsmall)) name(fig3_a)

 
drop _merge

merge m:1 Brand using "$P_Data_Processed/top5_brands.dta"

gener top5brand = (_merge==3)

keep if n_stations>2

gegen mean_top5_share = mean(share_adopters), by(year_month)
gegen min_top5_share = min(share_adopters), by( year_month)
gegen max_top5_share = max(share_adopters), by( year_month)
gegen p99_top5_share = pctile(share_adopters), by( year_month) p(99)
gegen p1_top5_share = pctile(share_adopters), by( year_month) p(1)
gegen p95_top5_share = pctile(share_adopters), by( year_month) p(95)
gegen p5_top5_share = pctile(share_adopters), by( year_month) p(5)


sort year_month Brand
by year_month: gener use_this_obs = 1 if _n==1

sort use_this_obs year_month

format year_month %tm 

tw (line mean_top5_share year_month if  use_this_obs==1, color(gray)) ///
(line p95_top5_share year_month if use_this_obs==1, color(gray) lpattern(dash)) /// 
(line p5_top5_share year_month if use_this_obs==1, color(gray) lpattern(dash)), legend(off) xtitle("Month") ytitle("Share of Adopting Stations") title("(b) All Brand Share Distribution", size(medsmall)) name(fig3_b)

 
graph combine fig3_a fig3_b, iscale(1) xsize(10)

gr export "$P_Figures/Figure_3.eps", replace cmyk(on)

********************
***** FIGURE A1 *****
********************

graph drop _all
* pccount picture * 
clear
use "$P_Data_Processed/pccount_breaks_e5.dta"
format wdate %tw
histogram wdate, color(gray) percent xtitle("Date") name(pccount_hist) title("(a) Number of Price Changes",size(medsmall))

* pcresponse picture *
clear
use "$P_Data_Processed/pcresponse_breaks_e5.dta"
format wdate %tw
histogram wdate, color(gray) percent xtitle("Date") name(pcresponse_hist) title("(b) Rival Response Time", size(medsmall))


** oil shock **
clear
use "$P_Data_Processed/oil_breaks_e5.dta"
format wdate %tw
histogram wdate, color(gray) percent xtitle("Date") name(oil_hist) title("(c) Responsiveness to Oil Price Shocks",size(medsmall))

** weather shock **
clear
use "$P_Data_Processed/weather_breaks_e5.dta"
format wdate %tw
histogram wdate, color(gray) percent xtitle("Date") name(weather_hist) title("(d) Responsiveness to Local Weather Shocks",size(medsmall))

 
* combine graphs * 

graph combine pccount_hist pcresponse_hist oil_hist weather_hist

gr export "$P_Figures/Figure_A1.eps", replace
